package com.tjmc.nsia.core.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjmc.nsia.core.dao.entity.NsiaChat;
import com.tjmc.nsia.core.dao.entity.NsiaChatSession;
import com.tjmc.nsia.core.dao.mapper.NsiaChatSessionMapper;
import com.tjmc.nsia.core.service.NsiaChatService;
import com.tjmc.nsia.core.service.NsiaChatSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2024/06/23
 */
@Service
public class NsiaChatSessionServiceImpl extends ServiceImpl<NsiaChatSessionMapper, NsiaChatSession> implements NsiaChatSessionService {

    @Autowired
    private NsiaChatSessionMapper nsiaChatSessionMapper;

    @Autowired
    private NsiaChatService nsiaChatService;

    @Override
    public NsiaChatSession updateChatSession(NsiaChat nsiaChat) {
        NsiaChatSession nsiaChatSession = null;
        if(!Objects.isNull(nsiaChat)) {
            boolean newChatSession = false;
            if(Objects.isNull(nsiaChat.getSessionId()) || nsiaChat.getSessionId() == 0) {
                newChatSession = true;
            } else {
                nsiaChatSession = nsiaChatSessionMapper.selectById(nsiaChat.getSessionId());
                if(!Objects.isNull(nsiaChatSession)) {
                    nsiaChatSession.setTitle(nsiaChat.getQuestion());
                    nsiaChatSession.setUserId(nsiaChat.getUserId());
                    nsiaChatSessionMapper.updateById(nsiaChatSession);
                } else {
                    newChatSession = true;
                }
            }
            if(newChatSession) {
                nsiaChatSession = new NsiaChatSession();
                nsiaChatSession.setUserId(nsiaChat.getUserId());
                nsiaChatSession.setTitle(nsiaChat.getQuestion());
                nsiaChatSession.setId(nsiaChat.getSessionId());
                nsiaChatSessionMapper.insert(nsiaChatSession);
            }
        }
        return nsiaChatSession;
    }

    @Override
    public void deleteChatSessions(Long userId) {
        if(!Objects.isNull(userId) && userId > 0) {
            List<NsiaChatSession> nsiaChatSessions = nsiaChatSessionMapper.selectList(new QueryWrapper<NsiaChatSession>().eq("user_id", userId));
            if(!CollectionUtil.isEmpty(nsiaChatSessions)) {
                nsiaChatSessions.stream().forEach(nsiaChatSession -> {
                    nsiaChatSessionMapper.deleteById(nsiaChatSession);
                    nsiaChatService.deleteChats(nsiaChatSession.getId());
                });
            }
        }
    }

    @Override
    public NsiaChatSession getLatest(Long userId) {
        if(!Objects.isNull(userId) && userId > 0) {
            return nsiaChatSessionMapper.selectOne(new QueryWrapper<NsiaChatSession>().eq("user_id", userId).orderByDesc("update_time").last("limit 1"));
        }
        return null;
    }
}
